//
//  HMWelcomeViewController.swift
//  HMWeiBo
//
//  Created by Apple on 16/7/31.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class HMWelcomeViewController: UIViewController {

    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupUI()
        
        // 调用动画
//        startAnimation()
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
//        startAnimation()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func setupUI(){
        
        view.addSubview(photoImageView)
        view.addSubview(messageLabel)
        
        // 设置约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            
            // 水平居中和距离底部
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(photoImageView.snp_bottom).offset(20)
        }
        
        
        
    }
    
    //MARK:-- 弹簧动画
    private func startAnimation(){
        
        /*
         usingSpringWithDamping: 弹簧效果 0~1.0 取值越小,弹簧效果越明显
        initialSpringVelocity:表示初始的速度
            OC里给枚举的默认值是 填写 0;swift中 用 []标示
        */
        
        // 我们想更新约束
        self.photoImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
        })
        
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // 约束动画需要view调用一下 layoutIfNeeded
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                printLog("动画完成")
                // 页面切换
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangeViewController, object: self)
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK:-- 懒加载控件
    
    ///背景图片
    private lazy var bgImageView: UIImageView = {
    
        let img = UIImageView()

        img.image = UIImage(named: "ad_background")
        
        return img
    }()
   
    /// 头像
    private lazy var photoImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        // 设置成圆的
        img.layer.cornerRadius = 170/2/2
        
        // 深灰色的圆圈
        img.layer.borderWidth = 2

        img.layer.borderColor = UIColor.yellowColor().CGColor
        
        img.layer.masksToBounds = true
        
//        img.clipsToBounds = true
        
        return img
        
    }()
    
    ///提示语
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "欢迎归来"
        
        label.font = UIFont.systemFontOfSize(16)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.textAlignment = .Center
        
        return label
        
    }()

}
